package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.dto.Dto;
import com.bean.pojo.*;
import com.bean.vo.ItripLabelDicVO;
import com.bean.vo.hotel.ItripSearchPolicyHotelVO;
import com.bean.vo.hotelroom.SearchHotelRoomVO;
import com.service.IItripHotelRoomService;
import com.service.IItripHotelTempStoreService;
import com.service.IItripImageService;
import com.service.IItripLabelDicService;
import com.util.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class HotelRoomController {

    @Autowired
    private IItripImageService iItripImageService;
    @Autowired
    private IItripLabelDicService iItripLabelDicService;
    @Autowired
    private IItripHotelRoomService itripHotelRoomService;
    @Autowired
    private IItripHotelTempStoreService iItripHotelTempStoreService;

    @GetMapping("/api/hotelroom/getimg/{targetId}")
    public Dto getimg(@PathVariable("targetId") String targetId) {
        System.out.println(">>> 根据targetId查询酒店房型图片(type=1)");
        System.out.println("targetId:" + targetId);

        if (targetId == null) {
            return DtoUtil.returnError("100302 ", "酒店房型id不能为空", "");
        }

        ItripImage iimg = new ItripImage();

        QueryWrapper<ItripImage> wrapper = new QueryWrapper<>();
        wrapper.eq("targetId",targetId);
        wrapper.eq("type",1);

        List<ItripImage> list =new ArrayList<>();

        try {
            list = iItripImageService.list(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100301  ", "系统异常,获取失败", "");
        }

        return DtoUtil.returnSuccess("查询成功", list);
    }

    @GetMapping("/api/hotelroom/queryhotelroombed")
    public Dto queryhotelroombed() {
        System.out.println(">>> 查询酒店房间床型列表ItripLabelDicVO");

        QueryWrapper<ItripLabelDic> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId",1);
        List<ItripLabelDic> list1 = null;

        try {
           list1 = iItripLabelDicService.list(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100305  ", "获取酒店房间床型失败", "");
        }

        List<ItripLabelDicVO> list = new ArrayList<>();

        for (ItripLabelDic idc:
        list1) {
            list.add(new ItripLabelDicVO(idc.getId(),idc.getName()));
        }

        return DtoUtil.returnSuccess("查询成功", list);
    }

    @PostMapping("/api/hotelroom/queryhotelroombyhotel")
    public @ResponseBody
    Dto queryhotelroombyhotel(@RequestBody SearchHotelRoomVO vo) {
        System.out.println(">>> 查询酒店房间列表SearchHotelRoomVO");
//                "endDate": "2021-07-10T12:13:46.000Z",
//                "hotelId": "1",
//                "isBook": null,
//                "isCancel": null,
//                "isHavingBreakfast": null,
//                "isTimelyResponse": null,
//                "payType": null,
//                "roomBedTypeId": null,
//                "startDate": "2021-07-09T12:13:46.000Z"

        System.out.println(vo);

        QueryWrapper<ItripHotelRoom> wrapper = new QueryWrapper<>();
        wrapper.eq(vo.getHotelId()!=null,"hotelId",vo.getHotelId());
        wrapper.eq(vo.getIsBook()!=null,"isBook",vo.getIsBook());
        wrapper.eq(vo.getIsCancel()!=null,"isCancel",vo.getIsCancel());
        wrapper.eq(vo.getIsHavingBreakfast()!=null,"isHavingBreakfast",vo.getIsHavingBreakfast());
        wrapper.eq(vo.getIsTimelyResponse()!=null,"isTimelyResponse",vo.getIsTimelyResponse());
        wrapper.eq(vo.getPayType()!=null,"payType",vo.getPayType());
        wrapper.eq(vo.getRoomBedTypeId()!=null,"roomBedTypeId",vo.getRoomBedTypeId());


        List<ItripHotelRoom> itripHotelRoomList = null;

        List<List<ItripHotelRoom>> ALLlist = new ArrayList<>();

        try {
            itripHotelRoomList = itripHotelRoomService.list(wrapper);
            for (ItripHotelRoom i:
                    itripHotelRoomList) {
                boolean isflag = true;
                //判断库存
                QueryWrapper<ItripHotelTempStore> wrapper2 = new QueryWrapper<>();
                wrapper2.eq(i.getHotelId()!=null,"hotelId",i.getHotelId());
                wrapper2.eq(i.getId()!=null,"roomId",i.getId());
                wrapper2.ge(vo.getStartDate()!=null,"recordDate",vo.getStartDate());
                wrapper2.le(vo.getEndDate()!=null,"recordDate",vo.getEndDate());
                List<ItripHotelTempStore> oneRoom = iItripHotelTempStoreService.list(wrapper2);
                if(oneRoom.size()==0){
                    isflag=false;
                }else{
                    for (ItripHotelTempStore it:
                            oneRoom) {
                        if(it.getStore()==0){
                            isflag=false;
                        }
                    }
                }

                if(!isflag) continue;

                List<ItripHotelRoom> tempList = new ArrayList<>();
                tempList.add(i);
                ALLlist.add(tempList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100305  ", "获取酒店房间床型失败", "");
        }

        return DtoUtil.returnSuccess("查询成功", ALLlist);
    }

}
